package com.iscc.maoke.dao.impl;

import com.iscc.maoke.dao.TypeDAO;
import com.iscc.maoke.domain.Type;
import com.iscc.maoke.util.DataSourceUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDAOImpl implements TypeDAO {
    @Override
    public int add(Type type) {
        String sql = "INSERT INTO tab_type(tname) VALUES(?)";

        int result = -1;
        try {
            Connection conn = DataSourceUtil.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setString(1, type.getTname());
            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }


        return result;
    }

    @Override
    public int update(Type type) {
        String sql = "UPDATE tab_type SET tname = ? WHERE tno =?";
        int result = -1;
        try {
            Connection conn = DataSourceUtil.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setString(1, type.getTname());
            psmt.setInt(2, type.getTno());
            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int del(int id) {
        String sql = "DELETE FROM tab_type WHERE tno = ?";
        int result = -1;
        try {
            Connection conn = DataSourceUtil.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setInt(1, id);
            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<Type> queryAll() {


        return null;
    }

    @Override
    public Type queryById(int id) {
        Type type = null;
        String sql = "SELECT * FROM tab_type where tno =?";

        try {
            Connection conn = DataSourceUtil.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setInt(1, id);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                type = new Type();
                type.setTno(rs.getInt(1));
                type.setTname(rs.getString(2));
                type.setCreateTime(rs.getString(3));
                type.setTnoParent(rs.getInt(4));
                type.setTlevel(rs.getInt(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }
        return type;
    }

    @Override
    public List<Type> queryAllTypeByCondition(String name) {
        List<Type> types = new ArrayList<>();
        String sql = "SELECT * FROM tab_type where 1=1";
        if (name != null && !"".equals(name.trim())) {
            sql += " and tname like concat('%',?,'%')";
        }

        try {
            Connection conn = DataSourceUtil.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (name != null && !"".equals(name.trim())) {
                psmt.setString(1, name);
            }
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                Type type = new Type();
                type.setTno(rs.getInt(1));
                type.setTname(rs.getString(2));
                type.setCreateTime(rs.getString(3));
                type.setTnoParent(rs.getInt(4));
                type.setTlevel(rs.getInt(5));
                types.add(type);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }
        return types;
    }
}
